package com.zh.note.huawei.twohundred.Excel单元格数值统计;

import java.util.regex.Pattern;

/**
 * @Author: zhanghuan
 * @date: 2023/8/9 15:26
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {

        // Scanner sc = new Scanner(System.in);
        // String input = sc.nextLine();
        // String[] params = input.split(" ");
        // int rows = Integer.parseInt(params[0]);
        // int cols = Integer.parseInt(params[1]);
        // String[][] matrix = new String[rows][cols];
        // for (int i = 0; i < rows; i++) {
        //     matrix[i] = sc.nextLine().split(" ");
        // }
        // String output = sc.nextLine();
        int rows = 5;
        int cols = 3;
        String[][] matrix = {{"10", "12", "=C5"},
                {"15", "5", "6"},
                {"7", "8", "=3+C2"},
                {"6", "=B2-A1", "=C2"},
                {"7", "5", "3"}};
        String output = "B2:C4";

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i][j].charAt(0) == '=') {
                    if (matrix[i][j].contains("+")) {
                        String[] op = matrix[i][j].split("\\+");
                        String op1 = op[0];
                        String op2 = op[1];

                        int num1, num2;
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op1.substring(1))) { // 判断有没有字母，如果没有则进入
                            num1 = Integer.parseInt(op1.substring(1));
                        } else {
                            // 判断有字母
                            num1 = Integer.parseInt(matrix[Integer.parseInt(op1.substring(
                                    2)) - 1][op1.charAt(1) - 'A']);
                        }

                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op2)) {
                            num2 = Integer.parseInt(op2);
                        } else {
                            num2 = Integer.parseInt(matrix[Integer.parseInt(op2.substring(
                                    1)) - 1][op2.charAt(0) - 'A']);
                        }

                        matrix[i][j] = Integer.toString(num1 + num2);
                    } else if (matrix[i][j].contains("-")) {
                        String[] op = matrix[i][j].split("-");
                        String op1 = op[0];
                        String op2 = op[1];

                        int num1, num2;
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op1.substring(1))) {
                            num1 = Integer.parseInt(op1.substring(1));
                        } else {
                            num1 = Integer.parseInt(matrix[Integer.parseInt(op1.substring(
                                    2)) - 1][op1.charAt(1) - 'A']);
                        }

                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op2)) {
                            num2 = Integer.parseInt(op2);
                        } else {
                            num2 = Integer.parseInt(matrix[Integer.parseInt(op2.substring(
                                    1)) - 1][op2.charAt(0) - 'A']);
                        }

                        matrix[i][j] = Integer.toString(num1 - num2);
                    } else {
                        //没有+，-的情况
                        String substring = matrix[i][j].substring(2);
                        matrix[i][j] = matrix[Integer.parseInt(substring) - 1][matrix[i][j].charAt(1) - 'A'];
                    }
                }
            }
        }


        String[] outputParams = output.split(":");
        int res = 0;
        for (int i = Integer.parseInt(outputParams[0].substring(1)) - 1; i < Integer.parseInt(outputParams[1].substring(1)); i++) {
            for (int j = outputParams[0].charAt(0) - 'A'; j <= outputParams[1].charAt(0) - 'A'; j++) {
                res += Integer.parseInt(matrix[i][j]);
            }
        }
        System.out.println(res);
    }
}